package com.zerdoor.controller;

import com.google.gson.Gson;
import com.zerdoor.model.Resources;
import com.zerdoor.model.VipSet;
import com.zerdoor.pagehelper.PageHelper;
import com.zerdoor.pagehelper.PageInfo;
import com.zerdoor.service.RbacResourcesService;
import com.zerdoor.util.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by xiaoji on 2015/2/2.
 * 用户访问资源信息管理
 */
@Controller
@RequestMapping("rbacResources")
public class RbacResourcesController {
    @Autowired
    private RbacResourcesService rbacResourcesService;

    @RequestMapping(value = "/index",method = RequestMethod.GET)
    public String index(){
        return "/rbac/resources";
    }

    @RequestMapping(value = "/view",method = RequestMethod.POST)
    @ResponseBody
    public String view(HttpServletRequest request,int page,int rows,int id){
        if(id == 0){
            PageHelper.startPage(page, rows);
            List<Map<String, Object>> list = rbacResourcesService.getRbacResources(id);
            PageInfo pageInfo = new PageInfo(list);
            Gson gson = new Gson();
            Map<String,Object> obj = new HashMap<String, Object>();
            obj.put("rows", getJson(list,false));
            obj.put("total", pageInfo.getTotal());
            String result = gson.toJson(obj);
            return result;
        }else{
            //添加子节点
            List<Map<String, Object>> list = rbacResourcesService.getRbacResources(id);
            Gson gson = new Gson();
            return gson.toJson(getJson(list,true));
        }
    }

    /**
     * 初始化list列表
     * @param list
     * @return
     */
    public List<Resources> getJson(List<Map<String, Object>> list,boolean isChild){
        List<Resources> list2 = new ArrayList<Resources>();
        for (Map<String, Object> temp : list){
            Resources r = new Resources();
            r.setId(Integer.parseInt(temp.get("id").toString()));
            r.setName(temp.get("name").toString());
            r.setDescription(temp.get("description").toString());
            r.setUrl(temp.get("url").toString());
            r.setParentId(Integer.parseInt(temp.get("parent").toString()));
            if(isChild){
                r.setState("");
            }
            list2.add(r);
        }
        return  list2;
    }
    /**
     * 添加资源
     * @param request
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST, produces = "text/htm;charset=UTF-8")
    @ResponseBody
    public Map<String,Object> add(HttpServletRequest request){
        Map<String, Object> result = new HashMap<String, Object>();
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("resources_id",null);
        params.put("resourcesName",request.getParameter("name"));
        params.put("description",request.getParameter("description"));
        params.put("url",request.getParameter("url"));
        params.put("parent",request.getParameter("parent"));
        try {
            rbacResourcesService.addRbacResources(params);
        } catch (Exception e) {
            result.put("errormsg","添加失败");
        }
        return result;
    }

    /**
     * 修改资源
     * @param request
     * @param id
     * @return
     */
    @RequestMapping(value = "/edit",method = RequestMethod.POST, produces = "text/htm;charset=UTF-8")
    @ResponseBody
    public Map<String, Object> edit(HttpServletRequest request,int id){
        Map<String, Object> result = new HashMap<String, Object>();
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("resourcesId",Integer.parseInt(request.getParameter("id")));
        params.put("resourcesName",request.getParameter("name"));
        params.put("description",request.getParameter("description"));
        params.put("url",request.getParameter("url"));
        try {
            rbacResourcesService.updateRbacResources(params);
        } catch (Exception e) {
            result.put("errormsg", "修改失败");
        }
        return result;
    }

    /**
     * 删除资源
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> delete(int id){
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            rbacResourcesService.deleteRbacResources(id);
        } catch (Exception e) {
            result.put("errormsg", "删除失败");
        }
        return result;
    }
}
